package com.nebula.dao.impl;

import com.nebula.entity.Role;
import com.nebula.dao.Database;
import com.nebula.dao.IRoleDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CcccX
 * @create 2019-01-15 14:31
 */
public class RoleDaoImpl extends BaseDaoImpl<Role> implements IRoleDao {

    @Override
    public Map<String, Object> selectRoleIdByNavIdFromRoleNavigation(String navId) {
        Map<String, Object> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        String sql = "select role_id from role_navigation where nav_id = ?";
        ResultSet rs = (ResultSet) Database.excute(sql, navId);
        try {
            while (rs.next()) {
                list.add(rs.getInt("role_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        map.put("roleId", list);
        sql = "select count(1) from role_navigation where nav_id = ?";
        rs = (ResultSet) Database.excute(sql, navId);
        try {
            while (rs.next()) {
                map.put("count",rs.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sql = "select count(1) from role";
        rs = (ResultSet) Database.excute(sql);
        try {
            while (rs.next()) {
                map.put("total",rs.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }
}
